package com.chiang.canvascliprect;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Region.Op;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MyView(this));
	}
	
	class MyView extends View{

		private Paint iPaint ;
		private int iCommonBlue ;
//		common_blue
		public MyView(Context context) {
			super(context);
			init(context);
		}
		
		public MyView(Context context, AttributeSet attrs) {
			super(context, attrs);
			init(context);
		}
		
		public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
			super(context, attrs, defStyleAttr);
			init(context);
		}
		
		private void init(Context context){
			iPaint = new Paint();
			iPaint.setAntiAlias(true);
			iPaint.setStrokeWidth(10);
			iPaint.setTextSize(30);
			iCommonBlue = context.getResources().getColor(R.color.common_blue);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			drawBg(canvas);  
	        drawDiff(canvas);  
	        drawReplace(canvas);  
	        drawUnion(canvas);  
	        drawXor(canvas);  
	        drawReverse(canvas);  
	        drawIntersect(canvas); 
		}
		
		private void drawBg(Canvas canvas){
			/**
			 * Saves the current matrix and clip onto a private stack. 
			 * Subsequent calls to translate,scale,rotate,skew,concat or 
			 * clipRect,clipPath will all operate as usual, but when the
			 * balancing call to restore() is made, those calls will be
			 * forgotten, and the settings that existed before the save() will be reinstated.
			 */
			canvas.save();
			/**Precontact the current matrix with the specified translation*/
			canvas.translate(30, 30);
			drawScene(canvas, "ClipRect");
			/**This call balances a previous call to save(), and is used to remove all modifications to
			 *the matrix/clip state since the last save call. It is an error to call restore() 
			 *more times than save() was called.
			 */
			canvas.restore();
		}
		
		private void drawScene(Canvas canvas , String text){
			canvas.clipRect(0, 0, 300, 300);
			canvas.drawColor(Color.WHITE);
			iPaint.setColor(Color.RED);
			canvas.drawLine(0, 0, 300, 300, iPaint);
			iPaint.setColor(iCommonBlue);
			canvas.drawCircle(150, 150, 100, iPaint);
			iPaint.setColor(Color.WHITE);
			canvas.drawText(text, 100, 150, iPaint);
		}
		/**
		 * 相减
		 * @param canvas
		 */
		private void drawDiff(Canvas canvas){
			canvas.save();
			canvas.translate(380, 30);
			canvas.clipRect(0, 0, 200, 200);
//			canvas.clipRect(300, 300, 400, 400, Op.DIFFERENCE);
			drawScene(canvas, "相减");
			canvas.restore();
		}
		
		/**
		 * //取代
		 * @param canvas
		 */
		private void drawReplace(Canvas canvas){
			
		}
		
		/**
		 * 并集
		 * @param canvas
		 */
		private void drawUnion(Canvas canvas){
			
		}
		
		/**
		 * 存异去同XOR 
		 * @param canvas
		 */
		private void drawXor(Canvas canvas){
			
		}
		
		/**
		 * Difference的相反运算，将后画的部分中去掉先前的部分
		 * @param canvas
		 */
		private void drawReverse(Canvas canvas){
			
		}
		
		/**
		 * 交集
		 * @param canvas
		 */
		private void drawIntersect(Canvas canvas){
			
		}
		
		/*
		 //取代  
    private void drawReplace(Canvas canvas){  
        Path path = new Path();  
        canvas.save();  
        canvas.translate(10, 160);  
        path.reset();  
        canvas.clipPath(path); // makes the clip empty  
        path.addCircle(50, 50, 50, Path.Direction.CCW);  
        canvas.clipPath(path, Region.Op.REPLACE);  
        drawScene(canvas);  
        canvas.restore();  
  
        canvas.save();  
        canvas.translate(10, 160);  
        canvas.clipRect(0, 0, 60, 60);  
        canvas.clipRect(40, 40, 100, 100, Region.Op.REPLACE);  
        drawScene(canvas);  
        canvas.restore();  
    }  
    //并集  
    private void drawUnion(Canvas canvas){  
        canvas.save();  
        canvas.translate(160, 160);  
        canvas.clipRect(0, 0, 60, 60);  
        canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);  
        drawScene(canvas);  
        canvas.restore();  
    }  
    //存异去同XOR  
    private void drawXor(Canvas canvas){  
        canvas.save();  
        canvas.translate(10, 310);  
        canvas.clipRect(0, 0, 60, 60);  
        canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);  
        drawScene(canvas);  
        canvas.restore();  
    }  
    //Difference的相反运算，将后画的部分中去掉先前的部分  
    private void drawReverse(Canvas canvas){  
        canvas.save();  
        canvas.translate(160, 310);  
        canvas.clipRect(0, 0, 60, 60);  
        canvas.clipRect(40, 40, 100, 100,  
        Region.Op.REVERSE_DIFFERENCE);  
        drawScene(canvas);  
        canvas.restore();  
  
//      canvas.save();  
//      canvas.translate(160, 310);  
//      canvas.clipRect(0, 0, 60, 60);  
//      canvas.clipRect(40, 40, 100, 100,  
//      Region.Op.INTERSECT);  
//      drawScene(canvas);  
//      canvas.restore();  
    }  
    //交集  
    private void drawIntersect(Canvas canvas){  
        canvas.save();  
        canvas.translate(10, 460);  
        canvas.clipRect(0, 0, 60, 60);  
        canvas.clipRect(40, 40, 100, 100,  
        Region.Op.INTERSECT);  
        drawScene(canvas);  
        canvas.restore();  
    }  
		 */
	}
}
